from config import dbCon

class role:
    def __init__(self, roleId=0, roleName=None, roleStatus=None):
        self.roleId=roleId
        self.roleName=roleName
        self.roleStatus=roleStatus
    def addRole(self, role):
        if role.roleName:
            count = dbCon.insert('role',
                         role_name=role.roleName,
                         role_status='V'
                         )
        return count
    def updateRoleStatus(self, roleId=None, roleName=None, roleStatus='I'):
        if roleId or roleName:
            count = dbCon.update('role', 
                                 role_status=roleStatus,
                                 where=('role_id=$v_role_id or role_name=$v_role_name'),
                                 vars={v_role_id:roleId, v_role_name:roleName})
        return count
    def getRole(self, roleStatus=None):
        if roleStatus:
            result = select('role',
                            where('role_status=roleStatus or roleStatus is null'))
        roleList = []
        if Len(result)>0:
            for x in result:
                if Len(x)>0:
                    role=role()
                    role.roleId = x['role_id']
                    role.roleName = x['role_name']
                    role.roleStatus = x['role_status']
                    roleList.__add__(x)
        return roleList
    def addRolePerm(self, roleId, permId):
        if roleID and permId:
            count = dbCon.insert('role_permission_link',
                         role_id=roleId,
                         permission_id=permId)
            return count
            
            